package com.lyf.network.practice.practice_6_28;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author LYF
 * @date 2022/6/28
 */
public class TcpBioServer {

    static void startServer() throws IOException {
        ServerSocket serverSocket = new ServerSocket(8848);
        System.out.println("start server::");
        while (true){
           Socket acceptSocket = serverSocket.accept();
           System.out.println("accepted::"+acceptSocket.getRemoteSocketAddress());
           InputStream inputStream = acceptSocket.getInputStream();

           System.out.println("received::");
           int len = 0;
           byte[] data = new byte[1024];

           len = inputStream.read(data);
           System.out.println(new String(data,0,len));

//           while((len = inputStream.read(data))!=-1){
//               System.out.println(new String(data,0,len));
//           }

           Scanner scanner = new Scanner(System.in);
           System.out.println("reply:");
           String reply = scanner.nextLine();
           OutputStream outputStream = acceptSocket.getOutputStream();
           outputStream.write(reply.getBytes());

           inputStream.close();
           outputStream.close();
           //acceptSocket.close();
        }
    }

    public static void main(String[] args) throws IOException {
        startServer();
    }
}
